//
//  LDStatusCell.swift
//  Weibo201655
//
//  Created by pro on 16/5/10.
//  Copyright © 2016年 LeeDan. All rights reserved.
//

import UIKit
import SnapKit

class LDStatusCell: UITableViewCell {
    
    //MARK:----------------------- 属性 ----------------------------------//
    //微博属性
    var status: LDStatus? {
        didSet {
            originalView.status = status
            
            //判断是否有转发微博
            if let retweetStatus = status?.retweeted_status {
                //有转发微博
                retweetView.hidden = false
                retweetView.retweetStatus = retweetStatus
                //更新底部约束
                topConstraintOfBottomView?.uninstall()
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.topConstraintOfBottomView = make.top.equalTo(retweetView.snp_bottom).constraint
                })

            } else {
                retweetView.hidden = true
                //更新底部约束
                topConstraintOfBottomView?.uninstall()
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.topConstraintOfBottomView = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    //底部约束
    private var topConstraintOfBottomView: Constraint?
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        addSubviewConstraints()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
    }
    
    private func addSubviewConstraints() {
        //originalView
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
        }
        //retweetView
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
            
           // make.height.equalTo(44)
        }
        //bottomView
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
            
            topConstraintOfBottomView = make.top.equalTo(retweetView.snp_bottom).constraint
        }
        //cell的底部约束
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.top.equalTo(self)
            
            make.bottom.equalTo(bottomView)
        }
    }
    
    //MARK: 懒加载
    private lazy var originalView: LDOriginalView = LDOriginalView()
    
    private lazy var retweetView: LDRetweetView = LDRetweetView()
    
    private lazy var bottomView: LDCellBottomView = LDCellBottomView()
}
